﻿@using Microsoft.AspNetCore.Builder;
@using Microsoft.Extensions.Options;
@using SimpleIdServer.CredentialIssuer.Domains;
@using SimpleIdServer.CredentialIssuer.Website.Stores.CredentialIssuer;
@inherits Fluxor.Blazor.Web.Components.FluxorComponent
@inject DialogService dialogService
@inject NotificationService notificationService
@inject IDispatcher dispatcher
@inject IOptions<RequestLocalizationOptions> requestLocalizationOptions

<RadzenStack Gap="1rem">
    <!-- Type -->
    <RadzenFormField Text="@Global.Type" Variant=Variant.Outlined>
        <ChildContent>
            <RadzenTextBox @bind-Value="@Form.Type"></RadzenTextBox>
        </ChildContent>
        <Helper>
            <RadzenText TextStyle="TextStyle.Caption">@Global.TypeHelper</RadzenText>
        </Helper>
    </RadzenFormField>
    <!-- Format -->
    <RadzenFormField Text="@Global.Format" Variant=Variant.Outlined>
        <ChildContent>
            <RadzenDropDown @bind-Value="@Form.Format"
                            Data="@SupportedFormats"/>
        </ChildContent>
        <Helper>
            <RadzenText TextStyle="TextStyle.Caption">@Global.CredentialConfigurationFormatHelper</RadzenText>
        </Helper>
    </RadzenFormField>
    <!-- Scope -->
    <RadzenFormField Text="@Global.Scope" Variant=Variant.Outlined>
        <ChildContent>
            <RadzenTextBox @bind-Value="@Form.Scope"></RadzenTextBox>
        </ChildContent>
        <Helper>
            <RadzenText TextStyle="TextStyle.Caption">@Global.CredentialConfigurationScopeHelper</RadzenText>
        </Helper>
    </RadzenFormField>
    <!-- Json Ld Context -->
    <RadzenFormField Text="@Global.JsonLdContext" Variant=Variant.Outlined>
        <ChildContent>
            <RadzenTextBox @bind-Value="@Form.JsonLdContext"></RadzenTextBox>
        </ChildContent>
        <Helper>
            <RadzenText TextStyle="TextStyle.Caption">@Global.JsonLdContextDescription</RadzenText>
        </Helper>
    </RadzenFormField>
    <!-- Base url -->
    <RadzenFormField Text="@Global.BaseUrl" Variant=Variant.Outlined>
        <ChildContent>
            <RadzenTextBox @bind-Value="@Form.BaseUrl"></RadzenTextBox>
        </ChildContent>
        <Helper>
            <RadzenText TextStyle="TextStyle.Caption">@Global.BaseUrlDescription</RadzenText>
        </Helper>
    </RadzenFormField>
    <div>
        <RadzenButton class="mb-1" Click="@(args => UpdateSettings())" Icon="save" Text="@Global.Save" ButtonStyle="ButtonStyle.Primary" Size="ButtonSize.Medium"></RadzenButton>
    </div>
    <!-- Translations -->
    <div>
        <RadzenButton class="mb-1" Click="@(args => AddDisplay())" Icon="add" Text="@Global.AddCredentialConfigurationDisplay" ButtonStyle="ButtonStyle.Primary" Size="ButtonSize.Medium"></RadzenButton>
        <RadzenRow Gap="1rem">            
            @foreach(var display in CredentialConfiguration.Displays)
            {
                <RadzenColumn Size="4">
                    <CredentialViewer ServerId="@CredentialConfiguration.ServerId" Display="@display">
                        <ActionsContent>
                            <RadzenButton Click=@((evt) => EditDisplay(display)) Variant="Variant.Text">@Global.Edit</RadzenButton>
                            <RadzenButton Click=@((evt) => Remove(display)) Variant="Variant.Text">@Global.Delete</RadzenButton>
                        </ActionsContent>
                    </CredentialViewer>
                </RadzenColumn>
            }
        </RadzenRow>
    </div>
</RadzenStack>

@code {
    [Parameter]
    public Domains.CredentialConfiguration CredentialConfiguration { get; set; }
    List<string> SupportedFormats = new List<string>
    {
        "ldp_vc",
        "jwt_vc_json-ld",
        "jwt_vc_json"
    };
    UpdateSettingsForm? Form { get; set; }

    record UpdateSettingsForm
    {
        public string Type { get; set; }
        public string Format { get; set; }
        public string Scope { get; set; }
        public string JsonLdContext { get; set; }
        public string BaseUrl { get; set; }
    }

    protected override void OnParametersSet()
    {
        base.OnParametersSet();
        if (CredentialConfiguration == null) return;
        Form = new UpdateSettingsForm
        {
            Type = CredentialConfiguration.Type,
            Format = CredentialConfiguration.Format,
            Scope = CredentialConfiguration.Scope,
            JsonLdContext = CredentialConfiguration.JsonLdContext,
            BaseUrl = CredentialConfiguration.BaseUrl
            };
        SubscribeToAction<UpdateCredentialDetailsSuccessAction>((act) =>
        {
            notificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Success, Summary = Global.CredentialConfigurationDetailsUpdated });
            StateHasChanged();
        });
        SubscribeToAction<AddCredentialDisplaySuccessAction>((act) =>
        {
            notificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Success, Summary = Global.CredentialConfigurationDisplayAdded });
            StateHasChanged();
        });
        SubscribeToAction<UpdateCredentialDisplaySuccessAction>((act) =>
        {
            notificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Success, Summary = Global.CredentialConfigurationDisplayUpdated });
            StateHasChanged();
        });
        SubscribeToAction<DeleteCredentialDisplaySuccessAction>((act) =>
        {
            notificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Success, Summary = Global.CredentialConfigurationDisplayRemoved });
            StateHasChanged();
        });
        SubscribeToAction<UpdateCredentialDetailsErrorAction>((act) =>
        {
            notificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Error, Summary = act.ErrorMessage });
            StateHasChanged();
        });
        SubscribeToAction<AddCredentialDisplayErrorAction>((act) =>
        {
            notificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Error, Summary = act.ErrorMessage });
            StateHasChanged();
        });
        SubscribeToAction<UpdateCredentialDisplayErrorAction>((act) =>
        {
            notificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Error, Summary = act.ErrorMessage });
            StateHasChanged();
        });
    }

    async void AddDisplay()
    {
        var result = (await dialogService.OpenAsync<CredentialConfigurationDisplayModal>(Global.AddCredentialConfigurationDisplay, new Dictionary<string, object>
        {
            { "Display", new CredentialConfigurationTranslation
                {
                    Locale = requestLocalizationOptions.Value.DefaultRequestCulture.Culture.ToString()
                }
            },
            { "ServerId", CredentialConfiguration.ServerId }
        })) as CredentialConfigurationTranslation;
        if (result == null) return;
        var act = new AddCredentialDisplayAction { BackgroundColor = result.BackgroundColor, Description = result.Description, Id = CredentialConfiguration.Id, Locale = result.Locale, LogoAltText = result.LogoAltText, LogoUrl = result.LogoUrl, Name = result.Name, TextColor = result.TextColor };
        dispatcher.Dispatch(act);
    }

    async void EditDisplay(CredentialConfigurationTranslation translation)
    {
        var clone = translation.Clone() as CredentialConfigurationTranslation;
        var result = (await dialogService.OpenAsync<CredentialConfigurationDisplayModal>(Global.EditCredentialConfigurationDisplay, new Dictionary<string, object>
        {
            { "Display", clone },
            { "ServerId", CredentialConfiguration.ServerId }
        })) as CredentialConfigurationTranslation;
        if (result == null) return;
        var act = new UpdateCredentialDisplayAction { DisplayId = translation.Id, BackgroundColor = result.BackgroundColor, Description = result.Description, Id = CredentialConfiguration.Id, Locale = result.Locale, LogoAltText = result.LogoAltText, LogoUrl = result.LogoUrl, Name = result.Name, TextColor = result.TextColor };
        dispatcher.Dispatch(act);
    }

    void Remove(CredentialConfigurationTranslation translation)
    {
        var act = new DeleteCredentialDisplayAction { DisplayId = translation.Id, Id = CredentialConfiguration.Id };
        dispatcher.Dispatch(act);
    }

    void UpdateSettings()
    {
        var act = new UpdateCredentialDetailsAction { BaseUrl = Form.BaseUrl, Format = Form.Format, Id = CredentialConfiguration.Id, JsonLdContext = Form.JsonLdContext, Scope = Form.Scope, Type = Form.Type };
        dispatcher.Dispatch(act);
    }
}